package com.qifan.energy;


import android.os.Bundle;

import com.chaychan.library.BottomBarItem;
import com.chaychan.library.BottomBarLayout;
import com.qifan.energy.base.BaseActivity;
import com.qifan.energy.R;
import com.qifan.energy.adapter.CustomFragmentAdapter;
import com.qifan.energy.bean.result.OSSPolicyResult;
import com.qifan.energy.fragment.HomeFragment;
import com.qifan.energy.fragment.MineFragment;
import com.qifan.energy.fragment.CartFragment;
import com.qifan.energy.fragment.StudyFragment;
import com.qifan.energy.fragment.TreeFragment;
import com.qifan.energy.global.Keys;
import com.qifan.energy.http.net.NetUtils;
import com.qifan.energy.http.response.MyObserver;
import com.qifan.energy.util.SharedPreferencesUtil;
import com.qifan.energy.util.ToastUtils;
import com.qifan.energy.widget.CustomViewPager;

import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;

import butterknife.BindView;

public class MainActivity extends BaseActivity {

    private long lastPressBackMills = 0;
    @BindView(R.id.vp_content)
    CustomViewPager viewPager;
    @BindView(R.id.bbl)
    BottomBarLayout bottomBarLayout;
    private List<Fragment> pageList = new ArrayList<Fragment>() {{
        add(HomeFragment.newInstance());
        add(CartFragment.newInstance());
        add(TreeFragment.newInstance());
        //add(StudyFragment.newInstance());
        add(MineFragment.newInstance());
    }};

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        viewPager.setEnableScroll(false);
        viewPager.setOffscreenPageLimit(pageList.size());
        viewPager.setAdapter(new CustomFragmentAdapter(getSupportFragmentManager(),0,pageList));
        bottomBarLayout.setViewPager(viewPager);//底部bottombar;
        bottomBarLayout.setOnItemSelectedListener(new BottomBarLayout.OnItemSelectedListener() {
            @Override
            public void onItemSelected(BottomBarItem bottomBarItem, int i, int i1) {
                if (i1 == 2){
                    //hideStatusAndResetFullScreen();
                    setStatusWithBlueColor();
                } else {
                    setStatusWithSystemColor();
                }
            }
        });
    }

    @Override
    protected void initData() {
        /* oss上传签名生成 暂时去掉*/
        aliyunOssPolicy();
    }

    private void aliyunOssPolicy(){
        NetUtils.aliyunOssPolicy(new MyObserver<OSSPolicyResult>(this) {
            @Override
            public void onSuccess(OSSPolicyResult result) {
                //保存OSS TOKEN信息保存本地
                SharedPreferencesUtil.putData(Keys.OSS_TOKEN_KEY,result);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                ToastUtils.showShort(errorMsg);
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - lastPressBackMills > 3000) {
            lastPressBackMills = System.currentTimeMillis();
            ToastUtils.showShort(getResources().getString(R.string.double_click_for_exit));
        } else {
            /*System.exit(0); 官网不推荐 动画不流畅 */
            super.onBackPressed();
        }
    }
}
